package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.combat.attr.CombatAttribute;
import com.xcity.game.employee.EmployeeService;
import com.xcity.game.employee.config.impl.EmployeeStarLevelUpConfigure;
import com.xcity.game.employee.response.EmployeeLevelUpRequired;
import com.xcity.game.employee.template.EmployeeStarUpTemplate;
import com.xcity.game.item.ItemInfo;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class EmployeeStarUpResponse extends Response {
	
	private long id;
	private int starLvl;
	private short[] attrsIndex;
	private Object[] attrs;
	private NextLvlRequired next;
	
	public static class NextLvlRequired {
		public List<ItemInfo> items = Collections.emptyList(); // 需求物品
		public List<ItemInfo> cards = Collections.emptyList(); // 需求卡牌
		public long gold;
	}
	
	public EmployeeStarUpResponse(int serial, Player player, Employee employee, EmployeeStarUpTemplate template, EmployeeService service) {
		super(serial);
		this.id = employee.getId();
		this.starLvl = employee.getStarLvl();
		CombatAttribute[] growthAttrs = template.getAttributes();
		this.attrsIndex = new short[growthAttrs.length];
		this.attrs = new Object[growthAttrs.length];
		for (int i = 0; i < growthAttrs.length; i++) {
			CombatAttribute refer = growthAttrs[i];
			this.attrsIndex[i] = (short) refer.getType().ordinal();
			this.attrs[i] = refer.format(employee.getAttributeValue(refer.getType()));
		}
		EmployeeStarUpTemplate nextTemplate = service.configure().getStarLevelUpConfigure().getNextLevelTemplate(employee);
		if (nextTemplate != null) {
			next = new NextLvlRequired();
			if (nextTemplate.getItemId() != 0 && nextTemplate.getItemCount() > 0) {
				next.items = Arrays.asList(new EmployeeLevelUpRequired(nextTemplate.getItemId(), nextTemplate.getItemCount(), player));
			}
			next.cards = new ArrayList<ItemInfo>(2);
			ItemInfo mainCard = EmployeeStarLevelUpConfigure.getWrappedMainCard(player, employee, nextTemplate); // 主卡
			if (mainCard != null) {
				next.cards.add(mainCard);
			}
			ItemInfo viceCard = EmployeeStarLevelUpConfigure.getWrappedViceCard(player, employee, nextTemplate); // 副卡
			if (viceCard != null) {
				next.cards.add(viceCard);
			}
			next.gold = nextTemplate.getGold();
		}
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getStarLvl() {
		return starLvl;
	}

	public void setStarLvl(int starLvl) {
		this.starLvl = starLvl;
	}

	public short[] getAttrsIndex() {
		return attrsIndex;
	}

	public void setAttrsIndex(short[] attrsIndex) {
		this.attrsIndex = attrsIndex;
	}

	public Object[] getAttrs() {
		return attrs;
	}

	public void setAttrs(Object[] attrs) {
		this.attrs = attrs;
	}

	public NextLvlRequired getNext() {
		return next;
	}

	public void setNext(NextLvlRequired next) {
		this.next = next;
	}
	
}
